package com.yozinga.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yozinga.R;
import com.yozinga.entity.Category;
import com.yozinga.util.ImageLoader;
import com.yozinga.util.SharedData;
import com.yozinga.util.ToolKit;

public class CategaryAdapter extends BaseAdapter{

	ArrayList<Category> cList;
	ArrayList<String> lList;
	ImageLoader mImageLoder;
	Context mContext;
	SharedData mSharedData;
	public CategaryAdapter(ArrayList<Category> categoryList,ArrayList<String> logoList,Context context) {
		cList = categoryList;
		lList = logoList;
		mContext = context;
		mImageLoder =new ImageLoader(mContext);
		mSharedData = new SharedData(context);
	}

	@Override
	public int getCount() {
		return cList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	public class ViewHolder{

		public TextView mBusinessName;
		public TextView mTagLine;
		public TextView mOffers;
		public ImageView mLogo;
		public TextView mDistance;
	}

	@Override
	public View getView(int position, View v, ViewGroup arg2) {
		ViewHolder holder ;

		if (v == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(mContext);
			v = inflater.inflate(R.layout.adapter_category_listitems,null);
			holder.mLogo =(ImageView)v.findViewById(R.id.ivLogo);
			holder.mBusinessName = (TextView) v.findViewById(R.id.tvMName);
			holder.mTagLine = (TextView) v.findViewById(R.id.tvMTag);
			holder.mOffers = (TextView) v.findViewById(R.id.tvOffers);
			holder.mDistance=(TextView) v.findViewById(R.id.tvDistance); 
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}  

		final Category bean = cList.get(position);


		holder.mBusinessName.setText(bean.getBusinessName());
		holder.mTagLine.setText(bean.getMerchantTagLine());

		
		if((!mSharedData.getLatitude().equals("null")&&!mSharedData.getLongitude().equals("null"))&&( !bean.getLatitude().equals("null") && !bean.getLongitude().equals("null"))){
			Log.i("Today","Latitude is: "+mSharedData.getLatitude()+" original "+bean.getLatitude());
			Log.i("Today","Longitude is: "+mSharedData.getLongitude()+"original "+bean.getLongitude());
			Double distance = ToolKit.distance(Double.parseDouble(mSharedData.getLatitude()),Double.parseDouble(mSharedData.getLongitude()),Double.parseDouble( bean.getLatitude()), Double.parseDouble(bean.getLongitude()), "K");
			String tempDistance = ToolKit.formatNumber(Double.toString(distance));
			holder.mDistance.setText("Distance:"+tempDistance+" km");
		}else{
			holder.mDistance.setText("Distance:");
		}
		
		holder.mOffers.setText("Offers:"+bean.getOfferDetails().size());
		//mImageLoder.DisplayImage(lList.get(position), holder.mLogo,mContext);
		return v;
	}


}
